//
//  BridgeViewController.m
//  reacticons
//
//  Created by Nong Nghia on 7/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BridgeViewController.h"
#import "RootViewController.h"
#import "AppDelegate.h"
#import "EGOCache.h"
@interface BridgeViewController ()

@end

@implementation BridgeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self updateDatabaseFinish:nil];
    
    [[(AppDelegate*)[[UIApplication sharedApplication] delegate] loadingView] showLoadingView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDatabaseFinish:) name:kUpdateDatabaseFinishNotification object:nil];
    

    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - update database
-(void)updateDatabaseFinish:(NSNotification*)notification{
    NSLog(@"%@",[NSDate date]);
    [[EGOCache currentCache] clearCache];
//    sleep(3);
    [self update];
}
-(void)update{
    NSLog(@"%@",[NSDate date]);

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kUpdateDatabaseFinishNotification object:nil];
    [[NSUserDefaults standardUserDefaults] setValue:@"SUCCESS" forKey:@"INIT_DATA"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    RootViewController *rvc=[[RootViewController alloc] init];

    [rvc setTitle:@"Reacticons"];
    
    [[(AppDelegate*)[[UIApplication sharedApplication] delegate] loadingView] stopLoadingView];   
    [self.navigationController pushViewController:rvc animated:NO];
}
@end
